package zhongbo.java.air.monitor.web;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.monitor.Service.IFlightActivityDailyService;
import zhongbo.java.air.monitor.model.FlightActivityDaily;

import java.util.Date;
import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 03:35:26
 */
@RestController
@RequestMapping("flightActivityDaily")
@Api(tags = "航迹数据接口")
public class FlightActivityDailyController {

    @Autowired
    private IFlightActivityDailyService flightActivityDailyService;

    @ApiOperation("查询当天分组统计结果")
    @GetMapping("getDailyStats")
    public ResultVo<List<FlightActivityDaily>> getDailyStats(String date) {
        // 如果不传, 则默认查当天的统计结果
        if (StrUtil.isBlank(date)) {
            date = DateUtil.today();
        }
        List<FlightActivityDaily> dailyList = flightActivityDailyService.getDailyStats(date);
        return ResultVo.ok(dailyList);
    }

    // todo 今日周二待做, 标准crud接口(包括分页)
    @ApiOperation("修改航迹数据")
    @PutMapping("edit")
    public ResultVo updateFlightActivityDaily(@RequestBody FlightActivityDaily edit) {
        boolean isUpdate = flightActivityDailyService.updateById(edit);
        if (edit.getId() == null || edit.getId().isEmpty()){
            throw new BusinessException("id不存在无法进行修改");
        }
        if (!isUpdate){
            throw new BusinessException("修改有误不存在航迹数据");
        }
        return ResultVo.ok("修改成功!");
    }

    @ApiOperation("添加航迹数据")
    @PostMapping("add")
    public ResultVo saveFlightActivityDaily(@RequestBody FlightActivityDaily entity) {
        entity.setCreatedTime(new Date());
        entity.setUpdatedTime(new Date());
        boolean isSave = flightActivityDailyService.save(entity);
        if (!isSave){
            throw new BusinessException("添加失败!");
        }
        return ResultVo.ok("修改成功!");
    }

    @ApiOperation("删除航迹数据")
    @DeleteMapping("delete")
    public ResultVo deleteFlightActivityDaily(String entityId) {
        boolean isDelete = flightActivityDailyService.removeById(entityId);
        if (!isDelete){
            throw new BusinessException("删除失败,不存该航迹数据!");
        }
        return ResultVo.ok("删除成功!");
    }

    @GetMapping("/findPage")
    @ApiOperation("分页查询")
    public ResultVo<PageVo<FlightActivityDaily>> page(Integer page, Integer pageSize
            , String icao, String startDate, String endDate
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc){
        return ResultVo.ok(flightActivityDailyService.pageQuery(page, pageSize
                , icao, startDate, endDate
                , keyword, fields, oderByField, orderByAsc));
    }

    @GetMapping("findOne")
    @ApiOperation("查询一条")
    ResultVo<FlightActivityDaily> findOne(String entityId) {
        FlightActivityDaily flightActivityDaily = flightActivityDailyService.getById(entityId);
        return ResultVo.ok(flightActivityDaily);
    }
}
